package fr.obsmip.sedoo.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

import fr.obsmip.sedoo.client.event.ActivityStartEvent;
import fr.obsmip.sedoo.client.event.UserLoginEvent;
import fr.obsmip.sedoo.client.event.UserLogoutEvent;
import fr.obsmip.sedoo.client.ui.menu.AdministrationMenu;
import fr.obsmip.sedoo.client.ui.menu.MetadataProviderMenu;
import fr.obsmip.sedoo.client.ui.menu.MetadataUserMenu;
import fr.obsmip.sedoo.client.ui.misc.RBVStackLayoutPanel;

public class MenuViewImpl extends AbstractStyledResizeComposite implements MenuView {

	 @UiField AdministrationMenu administrationMenu;
	 @UiField MetadataProviderMenu metadataProviderMenu;
	 @UiField MetadataUserMenu metadataUserMenu;
	 @UiField RBVStackLayoutPanel stackPanel;
	 
	 private final static int USER_MENU = 0;
	 private final static int PROVIDER_MENU = 1;
	 private final static int ADMINISTATOR_MENU = 2;
	
	private static MenuViewImplUiBinder uiBinder = GWT
			.create(MenuViewImplUiBinder.class);

	interface MenuViewImplUiBinder extends UiBinder<Widget, MenuViewImpl> {
	}

	public MenuViewImpl() {
		initWidget(uiBinder.createAndBindUi(this)); 
		applyCommonStyle();
		metadataProviderMenu.setVisible(false);
		administrationMenu.setVisible(false);
		hideAuthenticatedMenus();
	}

	@Override
	public void onNotification(UserLoginEvent event) {
		stackPanel.showStack(PROVIDER_MENU);
		metadataProviderMenu.setVisible(true);
		if (event.getUser().isAdmin())
		{
			stackPanel.showStack(ADMINISTATOR_MENU);
			administrationMenu.setVisible(true);
		}
		stackPanel.showWidget(USER_MENU);
	}

	@Override
	public void onNotification(UserLogoutEvent event) {
		hideAuthenticatedMenus();
	}

	private void hideAuthenticatedMenus() 
	{
		stackPanel.showWidget(USER_MENU);
		stackPanel.hideStack(PROVIDER_MENU);
		stackPanel.hideStack(ADMINISTATOR_MENU);
		metadataProviderMenu.setVisible(false);
		administrationMenu.setVisible(false);
	}

	@Override
	public void onNotification(ActivityStartEvent event) {
		if (event.getType() == ActivityStartEvent.ADMINISTRATOR_ACTIVITY)
		{
			stackPanel.showWidget(ADMINISTATOR_MENU);
		}
		else if (event.getType() == ActivityStartEvent.MEMBER_ACTIVITY)
		{
			stackPanel.showWidget(PROVIDER_MENU);
		}
		else
		{
			stackPanel.showWidget(USER_MENU);
		}
	}

	
}
